package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.GoodsFavoriteStatusEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.FavoriteService;

/**
 * 验证检查类
 * @author wushubin
 *
 */
@Controller
public class FavoriteController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(FavoriteController.class);

	private static final String HOME_VIEW = "favorite/index";
	private static final String HOME = "/favorite/index";
	private static final String LIST = "/favorite/list";
	private static final String DELETE = "/favorite/delete";
	private static final String ADD = "/favorite/add";
	
	@Resource
	private FavoriteService favoriteService;
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
	@RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 20);
    		
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<Map<String,Object>> object = favoriteService.getFavoriteList(pageNo, pageSize, user.getId());
    		model.addAttribute("list",object );
    		model.addAttribute("pageTurn",object.getPageTurn() );
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }
	
	@RequestMapping(DELETE)
	public JSONObject deleteFavorite(HttpServletRequest request){
		try {
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
    		
			int favoriteId = reqInfo.getIntParameter("favoriteId", -1);
			if(favoriteId <= 0) {
				throw new Exception("非法的参数!");
			}
	
			return favoriteService.deleteFavorite(user.getId(), favoriteId);
		} catch (Exception e) {
			log.error("func[deleteFavorite] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@RequestMapping(ADD)
	public JSONObject addFavorite(HttpServletRequest request){
		try {
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			
			int userId = user.getId();
			int originalGoodsId = reqInfo.getIntParameter(ApiUrlParamEnum.originalGoodsId.getParamName(), 0);
			String tagIds = reqInfo.getParameter(ApiUrlParamEnum.tagIds.getParamName(), "");
			String favoriteTag = reqInfo.getParameter(ApiUrlParamEnum.favoriteTag.getParamName(), "");
			int goodsId = reqInfo.getIntParameter(ApiUrlParamEnum.goodsId.getParamName(), 0);
			int mealId = reqInfo.getIntParameter(ApiUrlParamEnum.mealId.getParamName(), 0);
			int isPrivacy = reqInfo.getIntParameter(ApiUrlParamEnum.isPrivacy.getParamName(), GoodsFavoriteStatusEnum.FAVORITE_STATUS_NOT_PRIVACY.getId());
			int status = reqInfo.getIntParameter(ApiUrlParamEnum.status.getParamName(), GoodsFavoriteStatusEnum.FAVORITE_STATUS_NOT_DELETE.getId());
			String remark = reqInfo.getParameter(ApiUrlParamEnum.remark.getParamName(), "");
			
			return favoriteService.addFavorite(userId, originalGoodsId, tagIds, favoriteTag, goodsId,mealId, isPrivacy, status, remark);
		} catch (Exception e) {
			log.error("func[addFavorite] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@RequestMapping(LIST)
    public void list(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			//收藏列表模块
	    	PageList<Map<String, Object>> list = favoriteService.getFavoriteList(1, 3, user.getId());
	    	model.put("list", list);
			
		} catch (Exception e) {
			log.error("func[list] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
}
